

import java.awt.*;
import java.awt.event.*;
import java.util.LinkedList;

public class OvalTool extends Tool {

  protected Point startingMousePosition;
  protected Point currentMousePosition;
  protected Color saveColor;

  public OvalTool(DrawingCanvas canvas, LinkedList<ShapeObject> ShapeObjects, MDUser mdUser) {
	  super(canvas, ShapeObjects, mdUser);
  }

  
  protected void drawOval(int x, int y, int width, int height) {
    int ovalX = x;
    int ovalY = y;
    int ovalWidth = width;
    int ovalHeight = height;
    if (width < 0) {
      ovalX = x + width;
      ovalWidth = -width;
    } 
    if (height < 0) {
      ovalY = y + height;
      ovalHeight = -height;
    }
    Graphics iBGraphics = canvas.getImageBufferGraphics();
    iBGraphics.drawOval(ovalX, ovalY, ovalWidth, ovalHeight);
      
    canvas.repaint();
  }


 public void mousePressed(MouseEvent e)  {
   /* Establish starting point for next drawing */
   startingMousePosition = e.getPoint();
   currentMousePosition = startingMousePosition;
   Graphics iBGraphics = canvas.getImageBufferGraphics();
   saveColor = iBGraphics.getColor( );
   iBGraphics.setXORMode(Color.lightGray);
   iBGraphics.setColor(Color.white);
   drawOval(startingMousePosition.x,
		 startingMousePosition.y, 1,1);
						
 }

 public void mouseDragged(MouseEvent e)  {
   Point newMousePosition = e.getPoint();
   drawOval(startingMousePosition.x, startingMousePosition.y,
         	currentMousePosition.x -startingMousePosition.x +1,
		currentMousePosition.y - startingMousePosition.y +1);
   drawOval(startingMousePosition.x,startingMousePosition.y,
                 newMousePosition.x -startingMousePosition.x + 1,
		 newMousePosition.y- startingMousePosition.y + 1);
   /* update current mouse coordinates */
   currentMousePosition = newMousePosition;
 }


  public void mouseReleased(MouseEvent e) { 
		Point newMousePosition = e.getPoint();
		drawOval(startingMousePosition.x,startingMousePosition.y,
		                 newMousePosition.x -startingMousePosition.x + 1,
		                 newMousePosition.y- startingMousePosition.y + 1);
	    Graphics iBGraphics = canvas.getImageBufferGraphics();
		ShapeObjects.add(new OvalObject(startingMousePosition, currentMousePosition, saveColor));	    
		iBGraphics.setPaintMode();
		paintObjects(0);
		iBGraphics.setColor(saveColor);
  }

  public void keyPressed(KeyEvent e) {}

}
